All files / src/app/api/collections/[id]/ranking/pins/[itemId] route.ts

95.23% Statements 20/21
100% Branches 4/4
100% Functions 2/2
94.73% Lines 18/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41                        9x 9x 9x 9x 9x 9x 8x 8x 7x   1x           3x 3x 3x 3x 3x 3x 2x 2x          
import { NextRequest, NextResponse } from "next/server";
import { requireAuth } from "@/lib/auth";
import { getDb } from "@/lib/db";
import { requireApiCollectionAccess } from "@/server/dal/collection-access";
import { RankingController } from "@/server/api/ranking";
import { errorResponse } from "@/server/api/http-errors";
 
type Params = { params: Promise<{ id: string; itemId: string }> };
 
// Hand-place one piece in the live ranking (spec ยง4 "Adjusting"). The first pin
// lazily opens the curator's one draft; later pins reuse it. Authz before body.
export async function PUT(request: NextRequest, { params }: Params) {
  try {
    const { id, itemId } = await params;
    const user = await requireAuth();
    const db = getDb();
    const access = await requireApiCollectionAccess(db, id, user.id);
    if (access instanceof NextResponse) return access;
    const body: unknown = await request.json();
    await RankingController.fromDb(db).pin(access.id, user.id, itemId, body);
    return NextResponse.json({ ok: true });
  } catch (error) {
    return errorResponse(error);
  }
}
 
// Unpin: the piece goes back to its derived place. Idempotent.
export async function DELETE(_request: NextRequest, { params }: Params) {
  try {
    const { id, itemId } = await params;
    const user = await requireAuth();
    const db = getDb();
    const access = await requireApiCollectionAccess(db, id, user.id);
    if (access instanceof NextResponse) return access;
    await RankingController.fromDb(db).unpin(access.id, user.id, itemId);
    return NextResponse.json({ ok: true });
  } catch (error) {
    return errorResponse(error);
  }
}